package server.proxy;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;

import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;

import server.proxy.factory.ProxySingletoneFactory;

/**
 * This class perform shutdown tasks for the server.
 * 
 * @author Maxim
 *
 */
public class ShutdownTasks {

	
	public ShutdownTasks() throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeySpecException, FileNotFoundException, IOException, IllegalBlockSizeException, BadPaddingException {
	
		saveBlackListToFile();
		saveWhiteListToFile();
	}
	
	
	private void saveBlackListToFile() throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeySpecException, FileNotFoundException, IOException, IllegalBlockSizeException, BadPaddingException{
		
		String tmp = ProxySingletoneFactory.getBlackList().toString();
		
		TripleDES.encrypt(TripleDES.readKey(new File(Config.PASSWORD_FILE_NAME)), new ByteArrayInputStream(tmp.getBytes()), new FileOutputStream(Config.BLACK_LIST_FILE_NAME));
	}
	
	
	private void saveWhiteListToFile() throws InvalidKeyException, NoSuchAlgorithmException, IllegalBlockSizeException, NoSuchPaddingException, BadPaddingException, InvalidKeySpecException, FileNotFoundException, IOException{
		
		String tmp = ProxySingletoneFactory.getWhiteListSingletone().toString();
		
		TripleDES.encrypt(TripleDES.readKey(new File(Config.PASSWORD_FILE_NAME)), new ByteArrayInputStream(tmp.getBytes()), new FileOutputStream(Config.WHITE_LIST_FILE_NAME));
		
	}
}
